//
//  TestTransformCtr.m
//  TestDemo
//
//  Created by mokai on 14-10-8.
//  Copyright (c) 2014年 mokai. All rights reserved.
//

#import "TestTransformCtr.h"
#import <QuartzCore/QuartzCore.h>

@interface TestTransformCtr ()
@property (weak, nonatomic) IBOutlet UIImageView *image;

@property (weak, nonatomic) IBOutlet UITextField *rotateAngle;

@end

@implementation TestTransformCtr


/***
 
 CGAffineTransformMakeXXX 基于屏幕原点进行变换
 CGAffineTransformXXX 基于当前控件原点进行变换
 
 
 CGAffineTransformInvert     //返回Transformation的反向
 CGAffineTransformConcat   //合并两个Transformation
 
 检测一个Transformation 
 CGAffineTransformIsIdentity //检测一个Transformation是不是恒等变换，也就是说不变
 CGAffineTransformEqualToTransform   //检测两个Transformation是否相等。
 
 ***/

/***
 1、CGAffineTransform CGAffineTransformMakeRotation ( CGFloat angle);
 在这里可以看到参数并不是一个角度，但是它是把参数作为一个弧度，然后把弧度再转换为角度来处理，
 其结果就可能是将一个图片视图旋转了多少度。
 ***/
- (IBAction)rotate:(UIButton *)sender {
    CGAffineTransform ct = CGAffineTransformRotate(_image.transform,M_PI);//M_PI 逆时钟180、M_PI_2顺时90
    [self apply:ct];
}


- (IBAction)rotateCustomer:(id)sender {
    CAKeyframeAnimation *theAnimation = [CAKeyframeAnimation animation];
    
    //把值调换即可调整旋转方向
	theAnimation.values = [NSArray arrayWithObjects:
						   [NSValue valueWithCATransform3D:CATransform3DMakeRotation(6.26, 0,0,1)],
						   [NSValue valueWithCATransform3D:CATransform3DMakeRotation(3.13, 0,0,1)],
						   [NSValue valueWithCATransform3D:CATransform3DMakeRotation(0, 0,0,1)],
						   nil];
	//theAnimation.cumulative = YES;
	theAnimation.duration = 3;
	theAnimation.repeatCount = 1;
	//theAnimation.removedOnCompletion = YES;
	
    theAnimation.timingFunctions = [NSArray arrayWithObjects:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn],
                                    [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut],
                                    nil
                                    ];
	
	[_image.layer addAnimation:theAnimation forKey:@"transform"];
}



/***
 2、CGAffineTransform CGAffineTransformMakeScale (CGFloat sx, CGFloat sy);
 可以看到这个参数就少多了，也比较好理解，假设是一个图片视图引用了这个变换，
 那么图片的宽度就会变为  width*sx  ，对应高度变为  hight * sy。
 **/
- (IBAction)Scale:(UIButton *)sender {
    CGAffineTransform ct = CGAffineTransformScale(_image.transform,0.5, 0.5);
    [self apply:ct];
}

- (IBAction)scale1:(id)sender {
    CGAffineTransform ct = CGAffineTransformScale(_image.transform,2, 2);
    [self apply:ct];
}

/***
 3、CGAffineTransform CGAffineTransformMakeTranslation (CGFloat tx,CGFloat ty);
 这个就比较好理解了，假设是一个视图，那么它的起始位置 x 会加上tx , y 会加上 ty
 ***/
- (IBAction)translation:(id)sender {
    CGAffineTransform ct = CGAffineTransformTranslate(_image.transform, 0, 10);
    [self apply:ct];
    

}


-(void)apply:(CGAffineTransform)ct{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:2];
    [_image setTransform:ct];
    [UIView commitAnimations];
}


/**
 3D旋转
 ***/
- (IBAction)layerRotate:(id)sender {
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:2];
    
    CATransform3D rotationAndPerspectiveTransform = CATransform3DIdentity;
    rotationAndPerspectiveTransform.m34 = 1.0 / -500;
    rotationAndPerspectiveTransform = CATransform3DRotate(rotationAndPerspectiveTransform, 45.0f * M_PI / 180.0f, 0.0f, 1.0f, 0.0f);
    _image.layer.transform = rotationAndPerspectiveTransform;
    
    [UIView commitAnimations];
}







@end
